package com.mindskip.xzs.controller.student;


import com.mindskip.xzs.base.BaseApiController;
import com.mindskip.xzs.base.RestResponse;
import com.mindskip.xzs.service.FileUpload;
import com.mindskip.xzs.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;


@RequestMapping("/api/student/upload")
@RestController("StudentUploadController")
public class UploadController extends BaseApiController {

    private final FileUpload fileUpload;
    private final UserService userService;
    final String urlLocal = "http://ndjlba.com:9000";

    @Autowired
    public UploadController(FileUpload fileUpload, UserService userService) {
        this.fileUpload = fileUpload;
        this.userService = userService;
    }


    @RequestMapping("/image2")
    @ResponseBody
    public RestResponse questionUploadAndReadExcel(HttpServletRequest request) {
        MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
        MultipartFile multipartFile = multipartHttpServletRequest.getFile("file");
        long attachSize = multipartFile.getSize();
        String imgName = multipartFile.getOriginalFilename();
        try (InputStream inputStream = multipartFile.getInputStream()) {
            String filePath = fileUpload.uploadFile(inputStream, attachSize, imgName);
            userService.changePicture(getCurrentUser(), filePath);
            return RestResponse.ok(filePath);
        } catch (IOException e) {
            return RestResponse.fail(2, e.getMessage());
        }
    }

    @RequestMapping(value = "/media2", method = RequestMethod.POST)
    @ResponseBody
    public RestResponse uploadMedia(HttpServletRequest request) {
        MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
        MultipartFile multipartFile = multipartHttpServletRequest.getFile("file");
        long attachSize = multipartFile.getSize();
        String imgName = multipartFile.getOriginalFilename();
        try (InputStream inputStream = multipartFile.getInputStream()) {
            String filePath = fileUpload.uploadFile(inputStream, attachSize, imgName);
            userService.changePicture(getCurrentUser(), filePath);
            return RestResponse.ok(filePath);
        } catch (IOException e) {
            return RestResponse.fail(2, e.getMessage());
        }
    }


//    @RequestMapping(value = "/media2", method = RequestMethod.POST)
//    @ResponseBody
//    public RestResponse questionUploadAndMedia(@RequestParam("file") MultipartFile multipartFile, HttpServletRequest request) {
//        long attachSize = multipartFile.getSize();
//        String imgName = multipartFile.getOriginalFilename();
//        try (InputStream inputStream = multipartFile.getInputStream()) {
//            //视频上传
//            //获取原文件名
//            String name = multipartFile.getOriginalFilename();
//            //获取文件后缀
//            String subffix = name.substring(name.lastIndexOf(".") + 1, name.length());
//            //控制格式
//            if (subffix.equals("") || (!subffix.equals("mp4") && !subffix.equals("mov") && !subffix.equals("avi") && !subffix.equals("wmv") && !subffix.equals("m4v") && !subffix.equals("dat") && !subffix.equals("flv") && !subffix.equals("mkv"))) {
//                return RestResponse.fail(2, ("视频格式不对"));
//            }
//            //新的文件名以日期命名
//            String fileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
//            //获取项目路径到webapp
//            //String filepath = request.getServletContext().getRealPath("/") + "files\\";
//            //获取项目根路径并转到static/videos
//            String path = "D:\\media\\video";
//            File file = new File(path);
//            ////文件夹不存在就创建
//            if (!file.exists()) {
//                file.mkdirs();
//            }
//            //保存文件
//            try {
//                multipartFile.transferTo(new File(file + "\\" + fileName + "." + subffix));
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//            String realPath = file + "\\" + fileName + "." + subffix;
//            String simulationVideo = "/videos/" + fileName + "." + subffix;
//            return RestResponse.ok(realPath);
//        } catch (IOException e) {
//            return RestResponse.fail(2, e.getMessage());
//        }
//    }

    @RequestMapping(value = "/media", method = RequestMethod.POST)
    @ResponseBody
    public RestResponse questionUploadAndMedia(@RequestParam("file") MultipartFile multipartFile, HttpServletRequest request) {
        long attachSize = multipartFile.getSize();
        String imgName = multipartFile.getOriginalFilename();
        try (InputStream inputStream = multipartFile.getInputStream()) {
            //视频上传
            //获取原文件名
            String name = multipartFile.getOriginalFilename();
            //获取文件后缀
            String subffix = name.substring(name.lastIndexOf(".") + 1, name.length());
            //控制格式
            if (subffix.equals("") || (!subffix.equals("mp4") && !subffix.equals("mov") && !subffix.equals("avi") && !subffix.equals("wmv") && !subffix.equals("m4v") && !subffix.equals("dat") && !subffix.equals("flv") && !subffix.equals("mkv")) && !subffix.equals("MOV") ) {
                return RestResponse.fail(2, ("视频格式不对"));
            }
            //新的文件名以日期命名
            String fileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
            String path = "D:\\media\\image";
            File file = new File(path);
            ////文件夹不存在就创建
            if (!file.exists()) {
                file.mkdirs();
            }
            //保存文件
            try {
                multipartFile.transferTo(new File(file + "\\" + fileName + "." + subffix));
            } catch (IOException e) {
                e.printStackTrace();
            }
            String simulationVideo = urlLocal + "/" + fileName + "." + subffix;
            return RestResponse.ok(simulationVideo);
        } catch (IOException e) {
            return RestResponse.fail(2, e.getMessage());
        }
    }


    @RequestMapping("/image")
    @ResponseBody
    public RestResponse questionUploadAndReadExcel2(HttpServletRequest request) {
        MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
        MultipartFile multipartFile = multipartHttpServletRequest.getFile("file");
        long attachSize = multipartFile.getSize();
        String imgName = multipartFile.getOriginalFilename();
        String subffix = imgName.substring(imgName.lastIndexOf(".") + 1, imgName.length());
        String fileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        try (InputStream inputStream = multipartFile.getInputStream()) {
            //String filePath = fileUpload.uploadFile(inputStream, attachSize, imgName);
            //userService.changePicture(getCurrentUser(), filePath);
            String path = "D:\\media\\image";
            File file = new File(path);
            ////文件夹不存在就创建
            if (!file.exists()) {
                file.mkdirs();
            }
            //保存文件
            try {
                multipartFile.transferTo(new File(file + "\\" + fileName + "." + subffix));
            } catch (IOException e) {
                e.printStackTrace();
            }
            String simulationVideo = urlLocal + "/" + fileName + "." + subffix;
            userService.changePicture(getCurrentUser(), simulationVideo);
            return RestResponse.ok(simulationVideo);
        } catch (IOException e) {
            return RestResponse.fail(2, e.getMessage());
        }
    }


}
